
//分页参数
var pageIndex = 0;
//跳转链接
var urlType = 'new';
var url = urlPre+'/joke/list.future';
//是否还有
var hasMore = true;

function gotoUrl(type){
	if(type=='refresh'){
		location.href=urlPre+"/index.html";
	}else{
		urlType = type;
		hasMore = true;
		//清空列表
		$("#jokelist").html("");
		//重新加载
		pageInit();
		//将页面移动至顶部
		$("html,body").animate({scrollTop:$("#jokelist").offset().top-100},1);
	}
}

//加载更多
function more() {  
	if(!hasMore){
		return false;
	}
	$.ajax({
		url:url,
		cache:false,
		data:{
			pageIndex:pageIndex,
			pageSize:5,
			pageType:urlType
		},
		crossDomain:true,
		dataType:'jsonp',
		jsonpCallback:"jcb",
		success:function(data){
			if(data.length==0){
				hasMore = false;
				share.showMsg("没有更多了");
			}else{
				for(var i=0;i<data.length;i++){
					addList(data[i]);
				}
				$('#jokelist').listview('refresh');
				pageIndex++;
			}
		},
		error : function(msg) {  
            //alert("加载列表失败");  
        }  
	});
}

//首先要获取设备信息
function pageInit(){
	jQuery.ajax({
		url:url,
		data:{
			pageSize:5,
			pageType:urlType,
			uid:uid
		},
		async:false,
		cache:false,
		crossDomain:true,
		dataType:'jsonp',
		jsonpCallback:"jcb",
		success:function(data){
			for(var i=0;i<data.length;i++){
				addList(data[i]);
			}
			jQuery('#jokelist').listview('refresh');
			pageIndex++;
		},
		error : function(msg) {  
            //alert("初始化列表失败");  
        }  
	});
}

function addList(data){
	var content = $("#listContent").html();
	content = addListA(data,content);
	content = content.replace(/#content#/g,data.content);
	content = content.replace(/#likes#/g,data.likes);
	content = content.replace(/#nid#/g,data.nid);
	content = content.replace(/#id#/g,data.id);
	content = content.replace(/#title#/g,data.title);
	content = content.replace(/#store#/g,data.store);
	content = content.replace(/#share#/g,data.shareSinaweibo+data.shareWeixinpy+data.shareWeixinpyq);
	$("#jokelist").append(content);
}

function addListA(data,listStr){
	var content_a = $("#listContent_a").html();
	if(data.isLiked=="Y"){
		content_a = content_a.replace(/#likeMethod#/g,'doLikesCancel');
		content_a = content_a.replace(/#likesTheme#/g,'b');
	}else{
		content_a = content_a.replace(/#likeMethod#/g,'doLikes');
		content_a = content_a.replace(/#likesTheme#/g,'b');
	}
	if(data.isStored=="Y"){
		content_a = content_a.replace(/#storeMethod#/g,'storeCancel');
		content_a = content_a.replace(/#storeTheme#/g,'b');
	}else{
		content_a = content_a.replace(/#storeMethod#/g,'store');
		content_a = content_a.replace(/#storeTheme#/g,'b');
	}
	listStr = listStr.replace(/#content_a#/g,content_a);
	return listStr;
}

/**
*下拉加载
*/
$(document).on("scrollstop",function(){
	$("#popupMenu").popup("close");
	$("#detail").popup("close");
	if($(document).scrollTop() >= $(document).height()-$(window).height()){
		more();
	}
});

function shareback(nid,id){
	$("#share_"+nid).text(parseInt($("#share_"+nid).text())+1);
	$("#popupMenu").popup("close");
}

function doLikesCallBack(nid,id){
	$("#likes_"+nid).text(parseInt($("#likes_"+nid).text())+1);
	$("#likes_"+nid).attr('data-theme','c');
	$("#likes_"+nid).attr('href','javascript:doLikesCancel("'+nid+'","'+id+'")');
	$("#likes_"+nid).button('refresh');
}

function doLikesCancelBack(nid,id){
	$("#likes_"+nid).text(parseInt($("#likes_"+nid).text())-1);
	$("#likes_"+nid).attr('data-theme','b');
	$("#likes_"+nid).attr('href','javascript:doLikes("'+nid+'","'+id+'")');
	$("#likes_"+nid).button('refresh');
}

function storeBack(nid,id){
	$("#store_"+nid).text(parseInt($("#store_"+nid).text())+1);
	$("#store_"+nid).attr('href','javascript:storeCancel("'+nid+'","'+id+'")');
	$("#store_"+nid).attr('data-theme','c');
	$("#store_"+nid).button('refresh');
}

function storeCancelBack(nid,id){
	$("#store_"+nid).text(parseInt($("#store_"+nid).text())-1);
	$("#store_"+nid).attr('href','javascript:store("'+nid+'","'+id+'")');
	$("#store_"+nid).attr('data-theme','b');
	$("#store_"+nid).button('refresh');
}

function gotoDetail(id){
	location.href="detail.html?id="+id;
}

$(document).ready(function(){
	//获取设备信息
	initCommon();
});